/** 
 * 项目名:extWeb 
 * 文件名:UserController.java 
 * 包名:com.spring.extjs.controller 
 * 创建日期:2015年1月30日下午6:18:26 
 * 
 */

package com.spring.extjs.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.spring.extjs.model.TUser;
import com.spring.extjs.pageModel.UserModel;
import com.spring.extjs.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private IUserService userService;
	
	/**
	 * 回到用户管理的首页
	 */
	private final String LIST_ACTION = "redirect:/user";

	/**
	 * 进入系统用户列表<br>
	 * <功能详细描述>
	 * 
	 * @return
	 *
	 * @author chj_0507_dz@sina.com
	 */
	@RequestMapping
	public ModelAndView userList() {
		
		return new ModelAndView("userList");
	}

	/**
	 * 进入新增系统用户的界面<br>
	 * <功能详细描述>
	 * 
	 * @return
	 *
	 * @author chj_0507_dz@sina.com
	 */
	@RequestMapping("/new")
	public ModelAndView userNew() {
		
		return new ModelAndView("/newUser");
	}

	/**
	 * 保存一个系统用户<br>
	 * <功能详细描述>
	 * 
	 * @param userModel
	 * @return
	 *
	 * @author chj_0507_dz@sina.com
	 */
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public ModelAndView saveUser(@ModelAttribute("user") UserModel userModel) {
		TUser user = new TUser();
		BeanUtils.copyProperties(userModel, user);
		int resultFlag = userService.insertUser(user);
		if (resultFlag == 1) {
			return new ModelAndView(LIST_ACTION);

		} else {
			return new ModelAndView("/failure");
		}
	}

	/**
	 * 显示一个系统用户<br>
	 * <功能详细描述>
	 * 
	 * @param userModel
	 * @return
	 *
	 * @author chj_0507_dz@sina.com
	 */
	@RequestMapping("/{id}")
	public ModelAndView showUser(@PathVariable int id, HttpServletRequest request, HttpServletResponse response) {

		return new ModelAndView("/showUser");
	}

	/**
	 * 编辑一个系统用户<br>
	 * <功能详细描述>
	 * 
	 * @param userModel
	 * @return
	 *
	 * @author chj_0507_dz@sina.com
	 */
	@RequestMapping(value = "/{id}/edit")
	public ModelAndView editUser(@PathVariable int id, HttpServletRequest request, HttpServletResponse response) {

		return new ModelAndView("/editUser");
	}

	/** 保存更新 */
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public ModelAndView updateUser(@PathVariable int id, HttpServletRequest request, HttpServletResponse response) throws Exception {

		return new ModelAndView(LIST_ACTION);
	}

	/** 删除 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public ModelAndView deleteUser(@PathVariable int id, HttpServletRequest request, HttpServletResponse response) {

		return new ModelAndView(LIST_ACTION);
	}

	/** 批量删除 */
	@RequestMapping(method = RequestMethod.DELETE)
	public ModelAndView deleteUsers(HttpServletRequest request, HttpServletResponse response) {

		return new ModelAndView(LIST_ACTION);
	}
}
